package com.qu.tabhost.base;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import butterknife.ButterKnife;

/**
 * Created by quguangle on 2017/5/17.
 * fragment的基类
 */

public abstract class BaseFragment extends Fragment{
    private Context mContext;
    private Bundle bundle;
    protected View mRoot;
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mContext = context;
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mContext = null;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        bundle = getArguments();
        if(bundle != null){
            initBundle(bundle);
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        if(mRoot != null){
            ViewGroup viewGroup = (ViewGroup) mRoot.getParent();
            if(viewGroup != null)
            viewGroup.removeView(mRoot);
        }else{
            mRoot = inflater.inflate(getLayout(),container,false);
            //on butterkinfe before do something
            onBindViewBefore(mRoot);
            // bind view
            ButterKnife.bind(this,mRoot);
            initWidget(mRoot);
            initData();
        }
        return mRoot;
    }
    protected abstract int getLayout();
    protected void initBundle(Bundle bundle){
    }
    protected void onBindViewBefore(View mRoot){
    }
    protected void initWidget(View mRoot){
    }
    protected void initData(){
    }
}
